import cache from '@/cache/index'
import {passwordReg} from '@/common/constant'

export default {
  data() {
    var checkUserName = (rule, value, callback) => {
      if (
        value == null ||
        value == undefined ||
        value.length == 0
      ) {
        return callback(new Error("请填写用户名"));
      }
      callback();
    };
    var checkPassword = (rule, value, callback) => {
      if(passwordReg.test(value)){
        callback()
      }else{
        // callback(new Error('密码至少8位且包含大小写字母,数字和特殊符号'));
        callback()
      }
    };
    return {
      formLogin: {
        username: "",
        password: "",
      },
      rules: {
        username: [{
          required: true,
          message: '请输入用户名',
          trigger: "blur"
        }],
        password: [{
          validator: checkPassword,
          trigger: "blur"
        }],
      },
    };
  },
  methods: {
    login() {
      this.$refs['formLogin'].validate((valid) => {
        if (valid) {
          //密码md5加密传输
          this.$rq.httpPostForm(
            "/login",
            this.formLogin,
            {
              headers: {
                isToken: false
              }
            }
          ).then(result => {
              //将用户信息存储到本地
              cache.setToken(result.data)
              // 这里干脆使用最粗暴的页面重绘
              location.reload()
          }).catch(err => {
            console.error(err)
          });
        } else {
          return false;
        }
      });
    },
  },
  mounted() {
  },
};
